package services

import (
	"goravel/app/models"
	"regexp"
	"strings"
)

// 关键词：Overtaken、Abandoned、Overtaken.、Overtake
var bankMap = map[string]string{
	"manually destroyed": "销毁卡",
	"purchased":          "购买卡",
	"deposited":          "存钱",
	"withdrew":           "取钱",
	"cancelled":          "取消卡",
	"transferred":        "转账",
}

type BankRule struct {
}

/**
ActionTime    string `gorm:"column:action_time;type:datetime(3);not null" form:"action_time" json:"action_time"`
UserName      string `gorm:"column:user_name;type:varchar(255);not null" form:"user_name" json:"user_name"`
StreamId      string `gorm:"column:stream_id;type:varchar(255);not null" form:"stream_id" json:"stream_id"`
TradeType     string `gorm:"column:trade_type;type:varchar(255);not null" form:"trade_type" json:"trade_type"`
CardId        string `gorm:"column:card_id;type:varchar(255);not null" form:"card_id" json:"card_id"`
CardBalance   string `gorm:"column:card_balance;type:varchar(255);not null" form:"card_balance" json:"card_balance"`
CardType      string `gorm:"column:card_type;type:varchar(255);not null"form:"card_type" json:"card_type"`
Description   string `gorm:"column:description;type:varchar(255);null" form:"description" json:"description"`
Location      string `gorm:"column:location;type:varchar(255);null" form:"location" json:"location"`
OwnerId       string `gorm:"column:owner_id;type:varchar(255);null" form:"owner_id" json:"owner_id"`
OwnerName     string `gorm:"column:owner_name;type:varchar(255);null" form:"owner_name" json:"owner_name"`
OwnerStreamId string `gorm:"column:owner_stream_id;type:varchar(255);null" form:"owner_stream_id" json:"owner_stream_id"`
*/
// 前19个字符,日期时间
func (r *BankRule) GetActionTime(str string) string {
	return str[:19]
}

func (r *BankRule) HandleFields(str string, modelPrt *models.BankLog) *models.BankLog {
	if strings.Contains(str, "manually destroyed") {
		return r.destroyedHandleFields(str, modelPrt)
	}
	if strings.Contains(str, "purchased") {
		return r.purchasedHandleFields(str, modelPrt)
	}
	if strings.Contains(str, "cancelled") {
		return r.cancelledHandleFields(str, modelPrt)
	}
	if strings.Contains(str, "deposited") {
		return r.depositedHandleFields(str, modelPrt)
	}
	if strings.Contains(str, "withdrew") {
		return r.withdrewHandleFields(str, modelPrt)
	}
	if strings.Contains(str, "transferred") {
		return r.transferredHandleFields(str, modelPrt)
	}
	return nil
}

// 销毁卡
func (r *BankRule) destroyedHandleFields(str string, modelPrt *models.BankLog) *models.BankLog {
	//[Bank] 果汁(ID:76561198257021526)(Account Number:711900033719) manually destroyed Starter card belonging to Account Number:711900033719, at X=550602.375 Y=-408679.969 Z=4106.710.
	//获取models.Bank中的数据
	action_time := r.GetActionTime(str)
	//获取用户ID
	user_name := ""
	stream_id := ""
	trade_type := ""
	card_id := ""
	card_type := ""
	card_balance := ""
	location := ""
	orginal_text := ""
	//正则表达式
	reg := regexp.MustCompile(`\[Bank] (.*)\(ID:(.*)\)\(Account Number:(.*)\) manually destroyed (.*) card belonging to Account Number:.*, at\s+(.*)\.?`)
	//匹配
	if reg.MatchString(str) {
		match := reg.FindStringSubmatch(str)
		user_name = match[1]
		stream_id = match[2]
		card_id = match[3]
		card_type = match[4]
		location = match[5]
		trade_type = bankMap["manually destroyed"]
		orginal_text = match[0]
	}
	modelPrt.ActionTime = action_time
	modelPrt.UserName = user_name
	modelPrt.StreamId = stream_id
	modelPrt.TradeType = trade_type
	modelPrt.CardId = card_id
	modelPrt.CardBalance = card_balance
	modelPrt.Location = location
	modelPrt.CardType = card_type
	modelPrt.OriginalText = orginal_text
	return modelPrt
}

// 购买卡
func (r *BankRule) purchasedHandleFields(str string, modelPrt *models.BankLog) *models.BankLog {
	//[Bank] 哈喇子_(ID:76561199163094291)(Account Number:718600004087) purchased Classic card (free renewal: yes), new account balance is 314774 credits, at X=20806.150 Y=-676809.438 Z=345.170.
	//获取models.Bank中的数据
	action_time := r.GetActionTime(str)

	user_name := ""
	stream_id := ""
	trade_type := ""
	card_id := ""
	card_balance := ""
	location := ""
	description := ""
	orginal_text := ""
	//正则表达式
	reg := regexp.MustCompile(`\[Bank] (.*)\(ID:(.*)\)\(Account Number:(.*)\) purchased (.*) card \(free renewal: (.*)\), new account balance is (.*) credits, at\s+(.*)\.?`)
	//匹配
	if reg.MatchString(str) {
		match := reg.FindStringSubmatch(str)
		user_name = match[1]
		stream_id = match[2]
		trade_type = bankMap["purchased"]
		description = match[4] + "->" + match[5]
		card_id = match[3]
		card_balance = match[6]
		location = match[7]
		orginal_text = match[0]

	}
	modelPrt.ActionTime = action_time
	modelPrt.UserName = user_name
	modelPrt.StreamId = stream_id
	modelPrt.TradeType = trade_type
	modelPrt.CardId = card_id
	modelPrt.CardBalance = card_balance
	modelPrt.Location = location
	modelPrt.Description = description
	modelPrt.OriginalText = orginal_text
	return modelPrt
}

// 存钱
func (r *BankRule) depositedHandleFields(str string, modelPrt *models.BankLog) *models.BankLog {
	//[Bank] 無迪(ID:76561199252089993)(Account Number:717607038418) deposited 78435(76866 was added) to Account Number: 717607038418(無迪)(76561199252089993) at X=575034.938 Y=-227279.453 Z=360.040
	action_time := r.GetActionTime(str)

	user_name := ""
	stream_id := ""
	trade_type := ""
	card_id := ""
	card_balance := ""
	location := ""
	description := ""
	orginal_text := ""
	//正则表达式
	reg := regexp.MustCompile(`\[Bank] (.*)\(ID:(.*)\)\(Account Number:(.*)\) deposited (.*?) to Account Number:.*at\s+(.*)\.?`)
	//匹配
	if reg.MatchString(str) {
		match := reg.FindStringSubmatch(str)
		user_name = match[1]
		stream_id = match[2]
		trade_type = bankMap["deposited"]
		description = match[4]
		location = match[5]
		card_id = match[3]
		orginal_text = match[0]

	}
	modelPrt.ActionTime = action_time
	modelPrt.UserName = user_name
	modelPrt.StreamId = stream_id
	modelPrt.TradeType = trade_type
	modelPrt.CardId = card_id
	modelPrt.CardBalance = card_balance
	modelPrt.Location = location
	modelPrt.Description = description
	modelPrt.OriginalText = orginal_text

	return modelPrt
}

// 取钱
func (r *BankRule) withdrewHandleFields(str string, modelPrt *models.BankLog) *models.BankLog {
	//[Bank] 一字好(ID:76561198311656355)(Account Number:719207062794) withdrew 882(899 was removed) from Account Number: 719207062794(一字好)(76561198311656355) at X=575060.188 Y=-227280.734 Z=360.040
	action_time := r.GetActionTime(str)

	//获取用户ID
	user_name := ""
	stream_id := ""
	trade_type := ""
	card_id := ""
	card_balance := ""
	location := ""
	description := ""
	orginal_text := ""
	//正则表达式
	reg := regexp.MustCompile(`\[Bank] (.*)\(ID:(.*)\)\(Account Number:(.*)\) withdrew (.*)\((.*)\) from Account Number: .*at\s+(.*)\.?`)
	//匹配
	if reg.MatchString(str) {
		match := reg.FindStringSubmatch(str)
		user_name = match[1]
		stream_id = match[2]
		trade_type = bankMap["withdrew"]
		card_id = match[3]
		card_balance = match[4]
		description = match[5]
		location = match[6]
		orginal_text = match[0]

	}
	modelPrt.ActionTime = action_time
	modelPrt.UserName = user_name
	modelPrt.StreamId = stream_id
	modelPrt.TradeType = trade_type
	modelPrt.CardId = card_id
	modelPrt.CardBalance = card_balance
	modelPrt.Location = location
	modelPrt.Description = description
	modelPrt.OriginalText = orginal_text
	return modelPrt
}

// 取消卡
func (r *BankRule) cancelledHandleFields(str string, modelPrt *models.BankLog) *models.BankLog {
	//[Bank] 巴黎在逃小香猪(ID:76561199133020819)(Account Number:718201063483) cancelled Starter card at X=575055.188 Y=-227279.891 Z=360.040.
	action_time := r.GetActionTime(str)

	//获取用户ID
	user_name := ""
	stream_id := ""
	trade_type := ""
	card_id := ""
	card_type := ""
	location := ""
	orginal_text := ""
	//正则表达式
	reg := regexp.MustCompile(`\[Bank] (.*)\(ID:(.*)\)\(Account Number:(.*)\) cancelled (.*) card at\s+(.*)\.?`)
	//匹配
	if reg.MatchString(str) {
		match := reg.FindStringSubmatch(str)
		user_name = match[1]
		stream_id = match[2]
		trade_type = bankMap["cancelled"]
		card_id = match[3]
		card_type = match[4]
		location = match[5]
		orginal_text = match[0]
	}
	modelPrt.ActionTime = action_time
	modelPrt.UserName = user_name
	modelPrt.StreamId = stream_id
	modelPrt.TradeType = trade_type
	modelPrt.CardId = card_id
	modelPrt.Location = location
	modelPrt.CardType = card_type
	modelPrt.OriginalText = orginal_text

	return modelPrt
}

// 转账
func (r *BankRule) transferredHandleFields(str string, modelPrt *models.BankLog) *models.BankLog {
	//[Bank] 神神哒丶(ID:76561199350170889)(Account Number:717507115690) transferred 98000(102000 was removed) from Account Number: 717507115690(神神哒丶)(76561199350170889)
	//to Account Number: 719302113784(Sika_yu)(76561199350371985) at X=575070.312 Y=-227382.156 Z=360.040
	action_time := r.GetActionTime(str)

	//获取用户ID
	user_name := ""
	stream_id := ""
	trade_type := ""
	card_id := ""
	card_balance := ""
	location := ""
	owner_id := ""
	owner_name := ""
	owner_stream_id := ""
	description := ""
	orginal_text := ""
	//正则表达式
	reg := regexp.MustCompile(`\[Bank] (.*)\(ID:(.*)\)\(Account Number:(.*)\) transferred (.*)\((.*) was removed\) from Account Number: .* to Account Number:\s+(.*)\((.*)\)\((.*)\) at\s+(.*)\.?`)
	//匹配
	if reg.MatchString(str) {
		match := reg.FindStringSubmatch(str)
		user_name = match[1]
		stream_id = match[2]
		trade_type = bankMap["transferred"]
		card_id = match[3]
		card_balance = match[4]
		description = match[4] + "->" + match[5]
		owner_id = match[6]
		owner_name = match[7]
		owner_stream_id = match[8]
		location = match[9]
		orginal_text = match[0]
	}

	modelPrt.ActionTime = action_time
	modelPrt.UserName = user_name
	modelPrt.StreamId = stream_id
	modelPrt.TradeType = trade_type
	modelPrt.CardId = card_id
	modelPrt.CardBalance = card_balance
	modelPrt.Location = location
	modelPrt.OwnerId = owner_id
	modelPrt.OwnerName = owner_name
	modelPrt.OwnerStreamId = owner_stream_id
	modelPrt.Description = description
	modelPrt.OriginalText = orginal_text

	return modelPrt
}
